package com.example.demo.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.Student;
import com.example.demo.mapper.StuMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/student")
public class StuController {

    @Resource
    StuMapper stuMapper;


    @GetMapping("/stuFile/{id}")
    public Result<?> stuFile(@PathVariable Long id){
        System.out.println("前端传过来的id是:"+id);
//        QueryWrapper<Student> queryWrapper= new QueryWrapper<>();
//        queryWrapper.eq("id",id);
          Student student = stuMapper.selectById(id);

        return Result.success(student);
    }



    @PostMapping
    public Result<?> save(@RequestBody Student student) {

        stuMapper.insert(student);
        return Result.success();

    }
    @PutMapping
    public Result<?> update(@RequestBody Student student) {
        stuMapper.updateById(student);
        return Result.success();

    }

    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Long id) {
        stuMapper.deleteById(id);
        return Result.success();

    }

    @GetMapping("/Query")
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search) {

        System.out.println("sss");

        LambdaQueryWrapper<Student> wrapper = Wrappers.<Student>lambdaQuery();
        if (StrUtil.isNotBlank(search)) {
            wrapper.like(Student::getName, search);
        }
        Page<Student> stuPage = stuMapper.selectPage(new Page<>(pageNum, pageSize),wrapper);
        return Result.success(stuPage);

    }

    
}
